<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
<title></title>
</head>

<body>
<script type="text/javascript">
function ranStr(digit){
    var sourceStr="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G";    //定义字符串
    arrStr=sourceStr.split(",");//将字符串拆分成数组
    var result="";//定义变量并初始化
    var index=0;//定义变量并初始化
    for(i=0;i<digit;i++){
        index=parseInt(Math.random()*arrStr.length);//获取字符串随机索引
        result=result+arrStr[index];//对单个字符进行连接
    }
    alert(result);//输出结果
}
ranStr(6);
</script>
</body>
</html>
